import React, {Component} from 'react';
import ReactDom from 'react-dom';
import { FormattedMessage, injectIntl } from 'react-intl';
import {actions} from "mirrorx";
import {FormControl, Button, Modal, Message} from "tinper-bee";
import Radio from 'bee-radio';
import moment from 'moment'
import DatePicker from "bee-datepicker";
import cloneDeep from 'lodash.clonedeep';
import FormError from 'components/FormError';
import RefComponent from 'components/RefComponent';
import FormList from 'components/FormList'

let {RangePicker} = DatePicker;

import './index.less'

const FormItem = FormList.Item;

class CreateCopyLabel extends Component {
    constructor(props) {
        super(props);
        this.state = {
        }

    }
    
    //复制指定行
    copyToAssign=(getFieldProps,delFlag)=>{
        if (delFlag) {
            let rowNumStart = Number(getFieldProps("rowNumStart").value);
            let rowNumEnd = Number(getFieldProps("rowNumEnd").value);
            let data = cloneDeep(this.props.data)
            let copyData = [];
            if(!rowNumStart||!rowNumEnd){
                Message.create({ content: '请输入相关行号', color: 'warning' });
                this.props.closeCreateModal();
                return;
            }else{
                if(rowNumStart>rowNumEnd){
                    Message.create({ content: '开始行数必须小于等于截止行数！', color: 'warning' });
                    return;
                }
                if(rowNumStart>data.length){
                    Message.create({ content: '开始行数大于当前明细行数，请检查！', color: 'warning' });
                    return;
                }
                data.forEach(item=>{
                    delete item.id;
                    item.persistStatus = "new";
                    if(item.index>=rowNumStart&&item.index<=rowNumEnd){
                        copyData.push(item)
                    }
                })
            }
            this.props.updateCopyStatus({
                copying:true,
                selectData:copyData
            });
            this.props.closeCreateModal();
        }else{
            this.props.closeCreateModal();
        }   
    }

    render() {
        let {form,onCallback,showCreateCopyLabelModal} = this.props;
        let { getFieldProps, getFieldError } = this.props.form;

        let self = this;
        return (
            <Modal
            show={showCreateCopyLabelModal}
            onHide={this.CreateCopyLabel} >
            <Modal.Header>
                <Modal.Title>复制指定行</Modal.Title>
            </Modal.Header>

            <Modal.Body>
                <FormItem 
                    required={true}
                    label={'开始行数'}
                >
                        <FormControl
                            key={`rowNumStart`}
                            {...getFieldProps(`rowNumStart`, {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入",
                                    }
                                ],
                            })
                        }
                        />

                </FormItem>
                <FormItem 
                    required={true}
                    label={'截止行数'}
                >
                        <FormControl
                            key={`rowNumEnd`}
                            {...getFieldProps(`rowNumEnd`, {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入",
                                    }
                                ],
                            })
                        }
                        />

                </FormItem>
            </Modal.Body>

            <Modal.Footer>
                <Button onClick={() => this.copyToAssign(getFieldProps,false)} style={{ marginRight: 10 }}><FormattedMessage id="js.Ref.close" defaultMessage="关闭" /></Button>
                <Button onClick={() => this.copyToAssign(getFieldProps,true)} ><FormattedMessage id="js.Ref.chose" defaultMessage="确定" /></Button>
            </Modal.Footer>
        </Modal>

        )
    }
}

export default FormList.createForm()(injectIntl(CreateCopyLabel))
